#include <bits/stdc++.h>

#define in read()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

typedef unsigned long long ull;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 2e5 + 10;
const ull BASE = 19491001;

char s[N];
int a[N], pre[N][3], n, ans;
unordered_map < char , int > tran;
unordered_map < ull , int > mp;

void add(int x) {
	ull res = 0;
	rep(i, 1, 2) res = res * BASE + (pre[x][i] - pre[x][i - 1] + n + 100);
	if(mp.count(res)) ans = max(ans, x - mp[res]);
	else mp[res] = x;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
	tran['J'] = 0; tran['O'] = 1; tran['I'] = 2;
	n = in; scanf("%s",s + 1);
	add(0);
	rep(i, 1, n) {
		rep(j, 0, 2) pre[i][j] = pre[i - 1][j];
		pre[i][tran[s[i]]]++; add(i);
	}
	cout << ans << endl; return 0;
}
